package user;

import book.Library;
import constant.Constant;
import utils.NoPermission;

public class ProxyUser {
    private User realUser;
    private Library library = Library.getInstance();

    public ProxyUser(User user){
        this.realUser = user;
    }

    public User getRealUser() {
        return realUser;
    }

    public void setRealUser(User realUser) {
        this.realUser = realUser;
    }

    public Library getLibrary() {
        return library;
    }

    public void setLibrary(Library library) {
        this.library = library;
    }

    private void checkRealUserWhetherAdminUser(String exceptionMessage){
        if(!((this.realUser) instanceof AdminUser)){
            throw new NoPermission(exceptionMessage);
        }
    }

    // ----------------------------------管理员用户的方法----------------------------------------

    // 上架图书
    public void addBook(){
        System.out.println("代理类执行了...");
        checkRealUserWhetherAdminUser("普通用户没有添加图书的权限");
        ((AdminUser)this.realUser).addBook();
    }

    // 修改图书
    public void updateBook(){
        checkRealUserWhetherAdminUser("普通用户没有权限更新图书");
        ((AdminUser)realUser).updateBook();
    }

    // 删除书籍
    public void removeBook(){
        checkRealUserWhetherAdminUser("普通用户没有权限删除图书");
        ((AdminUser)realUser).removeBook();
    }

    // 统计每本书的借阅次数
    public void borrowCount(){
        checkRealUserWhetherAdminUser("普通用户没有权限查看图书的借阅次数");
        ((AdminUser)realUser).borrowCount();
    }

    // 查询最受欢迎的前n本书
    public void generateBook(){
        checkRealUserWhetherAdminUser("普通用户没有权限查看最受欢迎的前k本书");
        ((AdminUser)realUser).generateBook();
    }

    // 查看库存状态
    public void checkInventoryStatus(){
        checkRealUserWhetherAdminUser("普通用户没有权限查看库存状态");
        ((AdminUser)realUser).checkInventoryStatus();
    }

    // 并移除上架超过一年的图书
    public void checkAndRemoveOldBooks(){
        checkRealUserWhetherAdminUser("普通用户没有权限移除上架超过一年的图书");
        ((AdminUser)realUser).checkAndRemoveOldBooks();
    }

    // --------------------------------------------普通用户的方法---------------------------------------------

    private void checkRealUserWhetherNormalUser(String exceptionMessage){
        if(!((this.realUser) instanceof NormalUser)){
            throw new NoPermission(exceptionMessage);
        }
    }

    // 借阅图书
    public void borrowBook(){
        checkRealUserWhetherNormalUser("管理员请以普通用户的方式借阅图书");
        ((NormalUser)realUser).borrowBook();
    }

    // 归还图书
    public void returnBook(){
        checkRealUserWhetherNormalUser("管理员请以普通用户的方式归还图书");
        ((NormalUser)realUser).returnBook();
    }

    // 查看个人借阅情况
    public void viewBorrowHistory(){
        checkRealUserWhetherNormalUser("管理员请以普通用户的方式个人当前借阅情况");
        ((NormalUser)realUser).viewBorrowHistory();
    }

    public void handleOperation(int choice) {
        if(realUser instanceof AdminUser) {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                    break;
                default:
                    System.out.println("无效的操作:");

            }

        }else if(realUser instanceof NormalUser){
            switch (choice){
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowHistory();
                    break;
                default:
                    System.out.println("无效的操作。");
            }
        }
    }
}
